home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / MacApp Release 10 / MacApp Release 10 - HD Ready / Examples / Calc / UCalcRows.cp < prev    next >
Encoding:
Text File  |  1996-04-03  |  16.4 KB  |  547 lines  |  [TEXT/MPS ]

  1. //----------------------------------------------------------------------------------------
  2. // UCalcRows.cp
  3. // Copyright © 1986-96 by Apple Computer, Inc. All rights reserved.
  4. //----------------------------------------------------------------------------------------
  5.  
  6. #ifndef __UCALCROWS__
  7. #include "UCalcRows.h"
  8. #endif
  9.  
  10. // Calc
  11.  
  12. #ifndef __UCALCCOLUMNS__
  13. #include "UCalcColumns.h"
  14. #endif
  15.  
  16. #ifndef __UCALCROWS__
  17. #include "UCalcRows.h"
  18. #endif
  19.  
  20. #ifndef __UCALCCELLS__
  21. #include "UCalcCells.h"
  22. #endif
  23.  
  24. // MacApp
  25.  
  26. // Toolbox
  27.  
  28. // ANSI
  29.  
  30. #ifndef __STDIO__
  31. #include <stdio.h>
  32. #endif
  33.  
  34. #ifndef __STDLIB__
  35. #include <stdlib.h>
  36. #endif
  37.  
  38. //========================================================================================
  39. // CLASS TRowsView
  40. //========================================================================================
  41. #undef Inherited
  42. #define Inherited TTextGridView
  43.  
  44. #pragma segment ClassDescRes
  45. MA_DEFINE_CLASS_M1(TRowsView, Inherited);
  46.  
  47. //----------------------------------------------------------------------------------------
  48. // TRowsView::DoPostCreate: 
  49. //----------------------------------------------------------------------------------------
  50. #pragma segment AOpen
  51.  
  52. void TRowsView::DoPostCreate(TDocument*    itsDocument)    // override 
  53. {
  54.     fCalcDocument = (TCalcDocument *)(itsDocument);
  55.     fHelpID = kRowsViewHelp;
  56.     fHelpIndex = 2;
  57. } // TRowsView::DoPostCreate 
  58.  
  59. //----------------------------------------------------------------------------------------
  60. // TRowsView destructor
  61. //----------------------------------------------------------------------------------------
  62. #pragma segment MADestructorRes
  63.  
  64. TRowsView::~TRowsView()
  65. {
  66. }
  67.  
  68.  
  69. //----------------------------------------------------------------------------------------
  70. // TRowsView::AdornRow: 
  71. //----------------------------------------------------------------------------------------
  72. #pragma segment ARes
  73.  
  74. void TRowsView::AdornRow(short            /*aRow*/,
  75.                                 const VRect&     area)// override 
  76. {
  77.     PenSize(1, 1);
  78.     PenPat(&qd.black);
  79.  
  80.     CRect qdArea(ViewToQDRect(area));
  81.     // right line 
  82.     MoveTo(qdArea.right - 1, qdArea.top);
  83.     LineToPt(qdArea[botRight] - CPoint(1, 1));
  84.  
  85.     // bottom line 
  86.     MoveTo(qdArea.left, qdArea.bottom - 1);
  87.     LineToPt(qdArea[botRight] - CPoint(1, 1));
  88. } // TRowsView::AdornRow 
  89.  
  90.  
  91. //----------------------------------------------------------------------------------------
  92. // TRowsView::DoMouseCommand: 
  93. //----------------------------------------------------------------------------------------
  94. #pragma segment ASelCommand
  95.  
  96. void TRowsView::DoMouseCommand(VPoint&            theMouse,
  97.                                       TToolboxEvent*    event,
  98.                                       CPoint            /*hysteresis*/)    // override 
  99. {
  100.     TRowSelector * aRowSelector;
  101.  
  102.     aRowSelector = new TRowSelector;
  103.     aRowSelector->IRowSelector(fCalcDocument, this, theMouse, event->IsShiftKeyPressed(), event->IsCommandKeyPressed());
  104.     this->PostCommand(aRowSelector);
  105.     fCalcDocument->fSelectionType = kRowSelection;
  106.     fCalcDocument->fColumnsView->SetEmptySelection(kHighlight);
  107.     fCalcDocument->fColumnIsSelected = FALSE;
  108. } // TRowsView::DoMouseCommand 
  109.  
  110.  
  111. //----------------------------------------------------------------------------------------
  112. // TRowsView::DoSetCursor: 
  113. //----------------------------------------------------------------------------------------
  114. #pragma segment ARes
  115.  
  116. void TRowsView::DoSetCursor(const VPoint&    globalMouse,
  117.                                    RgnHandle        cursorRegion)// override 
  118. {
  119.     //!!! DoSetCursor = false; 
  120.     Inherited::DoSetCursor(globalMouse, cursorRegion);
  121. } // TRowsView::DoSetCursor 
  122.  
  123.  
  124. //----------------------------------------------------------------------------------------
  125. // TRowsView::DrawCell: 
  126. //----------------------------------------------------------------------------------------
  127. #pragma segment ARes
  128.  
  129. void TRowsView::DrawCell(GridCell        aCell,
  130.                                 const VRect&    aRect)// override 
  131. {
  132.     CStr255    theString;
  133.     NumToString(aCell.v, theString);
  134.  
  135.     CRect qdArea(this->ViewToQDRect(aRect));
  136.     qdArea.top = qdArea.top + 2;                // aesthetic adjustment of the CRect 
  137.     MADrawString(theString, qdArea, teCenter);
  138. } // TRowsView::DrawCell 
  139.  
  140.  
  141. //========================================================================================
  142. // CLASS TRow
  143. //========================================================================================
  144. #undef Inherited
  145. #define Inherited TObject
  146.  
  147. #pragma segment ClassDescRes
  148. MA_DEFINE_CLASS_M1(TRow, Inherited);
  149.  
  150. //----------------------------------------------------------------------------------------
  151. // TRow constructor 
  152. //----------------------------------------------------------------------------------------
  153. #pragma segment ARes
  154.  
  155. TRow::TRow()        
  156. {
  157.     fNumber = 0;
  158. } // TRow::TRow 
  159.  
  160. //----------------------------------------------------------------------------------------
  161. // TRow destructor
  162. //----------------------------------------------------------------------------------------
  163. #pragma segment MADestructorRes
  164.  
  165. TRow::~TRow()
  166. {
  167. }
  168.  
  169. //----------------------------------------------------------------------------------------
  170. // TRow::IRow: 
  171. //----------------------------------------------------------------------------------------
  172. #pragma segment ARes
  173.  
  174. void TRow::IRow(short number)
  175. {
  176.     this->IObject();
  177.  
  178.     fNumber = number;
  179. } // TRow::IRow 
  180.  
  181.  
  182. //----------------------------------------------------------------------------------------
  183. // TRow::ReadFromDisk: 
  184. //----------------------------------------------------------------------------------------
  185. #pragma segment AReadFile
  186.  
  187. void TRow::ReadFromDisk(TFile* aFile)
  188. {
  189.     RowDiskInfo    rowInfo;
  190.     long        count;
  191.  
  192.     count = sizeof(RowDiskInfo);
  193.     FailOSErr(aFile->ReadData(&rowInfo, count));
  194.     fNumber = rowInfo.number;
  195. } // TRow::ReadFromDisk 
  196.  
  197.  
  198. //----------------------------------------------------------------------------------------
  199. // TRow::WriteToDisk: 
  200. //----------------------------------------------------------------------------------------
  201. #pragma segment AWriteFile
  202.  
  203. void TRow::WriteToDisk(TFile* aFile)
  204. {
  205.     RowNumber    r;
  206.     RowDiskInfo    rowInfo;
  207.     long        count;
  208.  
  209.     // need to write our row number which is read in in TCalcDocument.DoRead 
  210.     r = fNumber;
  211.     count = sizeof(RowNumber);
  212.     FailOSErr(aFile->WriteData(&r, count));
  213.  
  214.     // now, write out the info that TRow.ReadFromDisk expects to see 
  215.     rowInfo.number = fNumber;
  216.     count = sizeof(RowDiskInfo);
  217.     FailOSErr(aFile->WriteData(&rowInfo, count));
  218. } // TRow::WriteToDisk 
  219.  
  220.  
  221. //----------------------------------------------------------------------------------------
  222. // TRow::ReadFromScrap: 
  223. //----------------------------------------------------------------------------------------
  224. #pragma segment AClipBoard
  225.  
  226. void TRow::ReadFromScrap(Handle theScrap,
  227.                                 long& scrapOffset)
  228. {
  229.     RowDiskInfo RowInfo;
  230.  
  231.     ReadScrap(theScrap, scrapOffset, (unsigned char*) & RowInfo, sizeof(RowInfo));
  232.     fNumber = RowInfo.number;
  233. } // TRow::ReadFromScrap 
  234.  
  235.  
  236. //----------------------------------------------------------------------------------------
  237. // TRow::WriteToScrap: 
  238. //----------------------------------------------------------------------------------------
  239. #pragma segment AClipBoard
  240.  
  241. void TRow::WriteToScrap(Handle    theScrap,
  242.                                long&    scrapOffset)
  243. {
  244.     RowDiskInfo RowInfo;
  245.  
  246.     RowInfo.number = fNumber;
  247.     WriteScrap(theScrap, scrapOffset, (unsigned char*) & RowInfo, sizeof(RowInfo));
  248. } // TRow::WriteToScrap 
  249.  
  250.  
  251. //========================================================================================
  252. // CLASS TRowList
  253. //========================================================================================
  254. #undef Inherited
  255. #define Inherited TSortedList
  256.  
  257. #pragma segment ClassDescRes
  258. MA_DEFINE_CLASS_M1(TRowList, Inherited);
  259.  
  260. //----------------------------------------------------------------------------------------
  261. // TRowList destructor
  262. //----------------------------------------------------------------------------------------
  263. #pragma segment MADestructorRes
  264.  
  265. TRowList::~TRowList()
  266. {
  267. }
  268.  
  269. //----------------------------------------------------------------------------------------
  270. // TRowList::IRowList: 
  271. //----------------------------------------------------------------------------------------
  272. #pragma segment ARes
  273.  
  274. void TRowList::IRowList()
  275. {
  276.     this->ISortedList();
  277. } // TRowList::IRowList 
  278.  
  279.  
  280. //----------------------------------------------------------------------------------------
  281. // TRowList::Compare: 
  282. //----------------------------------------------------------------------------------------
  283. #pragma segment ARes
  284.  
  285. CompareResult TRowList::Compare(TObject* item1,
  286.                                        TObject* item2)// override 
  287. {
  288.     // like TCellList and TColumnList, we are guaranteed a homogenous list (Rows this case)
  289.     // so typecasting is perfectly safe.
  290.  
  291.     if (((TRow *)(item1))->fNumber > ((TRow *)(item2))->fNumber)
  292.         return kItem1GreaterThanItem2;
  293.     else if (((TRow *)(item1))->fNumber < ((TRow *)(item2))->fNumber)
  294.         return kItem1LessThanItem2;
  295.     else
  296.         return kItem1EqualItem2;
  297. } // TRowList::Compare 
  298.  
  299.  
  300. //----------------------------------------------------------------------------------------
  301. // TRowList::GetRow: 
  302. //----------------------------------------------------------------------------------------
  303. #pragma segment ARes
  304.  
  305. TRow* TRowList::GetRow(RowNumber r)
  306. {
  307.     CCalcRowIterator iter(this);                // initialize the row iterator to find the row
  308.  
  309.     for (TRow * aRow = iter.FirstRow(); iter.More(); aRow = iter.NextRow())
  310.         if (aRow->fNumber == r)
  311.             return aRow;                        // as soon as we find it, leave the method call
  312.  
  313.     return NULL;                                // if we get here, that means that we didn't find it
  314. } // TRowList::GetRow 
  315.  
  316.  
  317. //========================================================================================
  318. // CLASS TRowSelector
  319. //========================================================================================
  320. #undef Inherited
  321. #define Inherited TCalcSelectCommand
  322.  
  323. #pragma segment ClassDescRes
  324. MA_DEFINE_CLASS_M1(TRowSelector, Inherited);
  325.  
  326. //----------------------------------------------------------------------------------------
  327. // TRowSelector::TRowSelector: 
  328. //----------------------------------------------------------------------------------------
  329. #pragma segment ASelCommand
  330.  
  331. TRowSelector::TRowSelector()
  332. {
  333.     fCellSelector = NULL;
  334. } // TRowSelector::TRowSelector 
  335.  
  336. //----------------------------------------------------------------------------------------
  337. // TRowSelector::IRowSelector: 
  338. //----------------------------------------------------------------------------------------
  339. #pragma segment ASelCommand
  340.  
  341. void TRowSelector::IRowSelector(TCalcDocument*    itsDocument,
  342.                                        TGridView*        itsView,
  343.                                        VPoint&            theMouse,
  344.                                        Boolean            theShiftKey,
  345.                                        Boolean            theCommandKey)
  346. {
  347.     this->ICellSelectCommand(itsView, theMouse, theShiftKey, theCommandKey);
  348.  
  349.     fCalcDocument = itsDocument;
  350.  
  351.     if (fCalcDocument->fSelectionType != kRowSelection)
  352.         theCommandKey = FALSE;
  353.  
  354.     MAVolatileInit(TCalcSelectCommand*, aCellSelector, NULL);
  355.  
  356.     FailInfo fi;
  357.     Try(fi)
  358.     {
  359.         aCellSelector = new TCalcSelectCommand;
  360.         aCellSelector->ICalcSelectCommand(itsDocument, itsDocument->fCellsView, theMouse, theShiftKey, theCommandKey);
  361.         fi.Success();
  362.     }
  363.     else
  364.     {
  365.         aCellSelector = (TCalcSelectCommand*) FreeIfObject(aCellSelector);
  366.         this->Free();
  367.         fi.ReSignal();
  368.     }
  369.     fCellSelector = aCellSelector;
  370. } // TRowSelector::IRowSelector 
  371.  
  372.  
  373. //----------------------------------------------------------------------------------------
  374. // TRowSelector::Free: 
  375. //----------------------------------------------------------------------------------------
  376. #pragma segment ARes
  377.  
  378. TRowSelector::~TRowSelector()            // override 
  379. {
  380.     fCellSelector = (TCalcSelectCommand *)(FreeIfObject(fCellSelector));
  381. } // TRowSelector::Free 
  382.  
  383.  
  384. //----------------------------------------------------------------------------------------
  385. // TRowSelector::ComputeAnchorCell: 
  386. //----------------------------------------------------------------------------------------
  387. #pragma segment ADoCommand
  388.  
  389. void TRowSelector::ComputeAnchorCell(GridCell& clickedCell)// override 
  390. {
  391.     Inherited::ComputeAnchorCell(clickedCell);
  392.  
  393.     fAnchorCell.h = 1;
  394.     clickedCell.h = 1;
  395.     fCellSelector->ComputeAnchorCell(clickedCell);
  396. } // TRowSelector::ComputeAnchorCell 
  397.  
  398.  
  399. //----------------------------------------------------------------------------------------
  400. // TRowSelector::ComputeNewSelection: 
  401. //----------------------------------------------------------------------------------------
  402. #pragma segment ADoCommand
  403.  
  404. void TRowSelector::ComputeNewSelection(GridCell& clickedCell)// override 
  405. {
  406.     CRect r;
  407.  
  408.     clickedCell.h = fGridView->fNumOfCols;
  409.     Inherited::ComputeNewSelection(clickedCell);
  410.  
  411.     clickedCell.h = fCalcDocument->fNoOfColumns;
  412.     fCellSelector->ComputeNewSelection(clickedCell);
  413. } // TRowSelector::ComputeNewSelection 
  414.  
  415.  
  416. //----------------------------------------------------------------------------------------
  417. // TRowSelector::DoIt: 
  418. //----------------------------------------------------------------------------------------
  419. #pragma segment ADoCommand
  420.  
  421. void TRowSelector::DoIt()            // override 
  422. {
  423.     Inherited::DoIt();
  424.     fCellSelector->DoIt();
  425. } // TRowSelector::DoIt 
  426.  
  427.  
  428. //----------------------------------------------------------------------------------------
  429. // TRowSelector::TrackMouse: 
  430. //----------------------------------------------------------------------------------------
  431. #pragma segment ADoCommand
  432.  
  433. TTracker* TRowSelector::TrackMouse(TrackPhase    aTrackPhase,
  434.                                           VPoint&         /*anchorPoint*/,
  435.                                           VPoint&         /*previousPoint*/,
  436.                                           VPoint&        nextPoint,
  437.                                           Boolean        mouseDidMove)// override 
  438. {
  439.     GridCell clickedCell;
  440.  
  441.     if (mouseDidMove)
  442.     {
  443.         clickedCell = fGridView->VPointToCell(nextPoint);
  444.         if (aTrackPhase == trackBegin)
  445.         {
  446.             this->ComputeAnchorCell(clickedCell);
  447.             if (fCommandKey)
  448.             {
  449.                 fDeselecting = PtInRgn(fAnchorCell, fGridView->fSelections);
  450.                 fCellSelector->fDeselecting = fDeselecting;
  451.             }
  452.         }
  453.  
  454.         if (clickedCell != fPreviousCell)
  455.         {
  456.             this->ComputeNewSelection(clickedCell);
  457.             this->HighlightNewSelection();
  458.             fCellSelector->HighlightNewSelection();
  459.  
  460.             CopyRgn(fThisSelection, fPreviousSelection);
  461.             fPreviousCell = clickedCell;
  462.             CopyRgn(fCellSelector->fThisSelection, fCellSelector->fPreviousSelection);
  463.             fCellSelector->fPreviousCell = clickedCell;
  464.         }
  465.     }
  466.     return this;
  467. } // TRowSelector::TrackMouse 
  468.  
  469.  
  470.  
  471. //========================================================================================
  472. // CLASS CCalcRowIterator
  473. //========================================================================================
  474. #undef Inherited
  475.  
  476. //----------------------------------------------------------------------------------------
  477. // CCalcRowIterator::CCalcRowIterator: 
  478. //----------------------------------------------------------------------------------------
  479. #pragma segment IteratorRes
  480.  
  481. CCalcRowIterator::CCalcRowIterator(TCalcDocument* theCalcDocument) :
  482.     CObjectIterator(theCalcDocument ? theCalcDocument->fRows : NULL)
  483. {
  484. } // CCalcRowIterator::CCalcRowIterator 
  485.  
  486.  
  487. //----------------------------------------------------------------------------------------
  488. // CCalcRowIterator::CCalcRowIterator: 
  489. //----------------------------------------------------------------------------------------
  490. #pragma segment IteratorRes
  491.  
  492. CCalcRowIterator::CCalcRowIterator(TRowList* theRowList) :
  493.     CObjectIterator(theRowList)
  494. {
  495. } // CCalcRowIterator::CCalcRowIterator 
  496.  
  497.  
  498. //----------------------------------------------------------------------------------------
  499. // CCalcRowIterator::~CCalcRowIterator: 
  500. //----------------------------------------------------------------------------------------
  501. #pragma segment IteratorRes
  502.  
  503. CCalcRowIterator::~CCalcRowIterator()
  504. {
  505. } // CCalcRowIterator::~CCalcRowIterator 
  506.  
  507.  
  508. //----------------------------------------------------------------------------------------
  509. // CCalcRowIterator::CurrentRow: 
  510. //----------------------------------------------------------------------------------------
  511. #pragma segment IteratorRes
  512.  
  513. TRow* CCalcRowIterator::CurrentRow()
  514. {
  515.     // returns the current Row
  516.     return (TRow *)this->CurrentObject();
  517. } // CCalcRowIterator::CurrentRow 
  518.  
  519.  
  520. //----------------------------------------------------------------------------------------
  521. // CCalcRowIterator::FirstRow: 
  522. //----------------------------------------------------------------------------------------
  523. #pragma segment IteratorRes
  524.  
  525. TRow* CCalcRowIterator::FirstRow()
  526. {
  527.     // return the first Row in the iteration
  528.     return (TRow *)this->FirstObject();
  529. } // CCalcRowIterator::FirstRow 
  530.  
  531.  
  532. //----------------------------------------------------------------------------------------
  533. // CCalcRowIterator::NextRow: 
  534. //----------------------------------------------------------------------------------------
  535. #pragma segment IteratorRes
  536.  
  537. TRow* CCalcRowIterator::NextRow()
  538. {
  539.     // advances the iteration and then returns the Row
  540.     return (TRow *)this->NextObject();
  541. } // CCalcRowIterator::NextRow 
  542.  
  543. //----------------------------------------------------------------------------------------
  544. // End of UCalcRows.cp
  545.  
  546. #pragma segment Inline
  547.